﻿$ErrorActionPreference = "stop"
$rootDir = (Split-Path -parent $MyInvocation.MyCommand.Definition)
$screenSaverFile = "$rootDir\WebFlipScreenSaver.scr"
try {
	$a = New-Object -ComObject Scripting.FileSystemObject
	$f = $a.GetFile($screenSaverFile)
	$screenSaverShortPath = $f.ShortPath
} catch {
	Write-Error ("Unable to access file: $screenSaverFile`n" + $_.Exception.ToString())
}

try {
	. "${env:windir}\system32\rundll32.exe" desk.cpl,InstallScreenSaver $screenSaverShortPath
} catch {
	#fall back to modifying the registry
	$objUser = New-Object System.Security.Principal.NTAccount([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
	if($objUser) {
		$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
		$sid = $strSID.Value
		[Microsoft.Win32.RegistryKey]$key = [Microsoft.Win32.Registry]::Users.OpenSubKey("$sid\Control Panel\Desktop", $true)
		$key.SetValue("SCRNSAVE.EXE", $screenSaverShortPath);
	} 
}


